package system

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	s1 "MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"time"
)

type SysRoleCtrl struct {
	base.BaseCtrl
}

func (s *SysRoleCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleSysRoleMsg)
	return msg
}

// AddRole 添加角色
// @Tags 角色管理
// @Summary 添加角色
// @Security ApiKeyAuth
// @Param data body s1.SysRoleAddReq true "角色信息"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"添加成功"}"
// @Router /system/role [post]
func (s *SysRoleCtrl) AddRole(c *gin.Context) {
	addReq := &s1.SysRoleAddReq{}
	if err := c.ShouldBindJSON(addReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	addReq.CreatedBy = utils.GetUsernameFromGin(c)
	addReq.UpdatedBy = addReq.CreatedBy
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.AddRole(addReq); err != nil {
		global.MoLogger.Error("添加角色失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.ServerOccurredErrorCode, consts.TKey_RoleAddFailMsg)
		return
	} else {
		if ok {
			response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
			return
		} else {
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_RoleAddFailMsg)
		}
	}
}

// DeleteById 根据id删除角色
// @Tags 角色管理
// @Summary 根据id删除角色
// @Security ApiKeyAuth
// @Param id path string true "角色id"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"删除成功"}"
// @Router /system/role/{id} [delete]
func (s *SysRoleCtrl) DeleteById(c *gin.Context) {
	if ids, err := utils.TextToUint64s(c.Param("id"), ","); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	} else {
		if ok, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.DeleteRoleByIds(c, ids); err != nil {
			global.MoLogger.Error("删除角色失败", zap.Error(err))
			response.FailErrorWithCodeAsI18n(c, err, consts.CommDeleteFailCode, consts.TKey_RoleDeleteFailMsg)
			return
		} else if !ok {
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_CommDeleteFailMsg)
			return
		} else {
			response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
			return
		}
	}
}

// UpdateRoleById 更新角色
// @Tags 角色管理
// @Summary 更新角色
// @Security ApiKeyAuth
// @Param data body s1.SysRoleUpdateReq true "角色信息"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"更新成功"}"
// @Router /system/role [put]
func (s *SysRoleCtrl) UpdateRoleById(c *gin.Context) {
	updateReq := s1.SysRoleUpdateReq{}
	if err := c.ShouldBindJSON(&updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.UpdateRoleById(c, &updateReq); err != nil {
		global.MoLogger.Error("更新角色失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_RoleUpdateFailMsg)
	} else if !ok {
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_CommUpdateFailMsg)
	} else {
		//TODO 更新缓存用户权限

		response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
	}
}

// UpdateRoleStatus 更新角色状态
// @Tags 角色管理
// @Summary 更新角色状态
// @Security ApiKeyAuth
// @Param data body s1.ChangeRoleStatusReq true "角色信息"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"更新成功"}"
// @Router /system/role/changeStatus [put]
func (s *SysRoleCtrl) UpdateRoleStatus(c *gin.Context) {
	req := s1.ChangeRoleStatusReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	req.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.UpdateRoleStatus(c, &req); err != nil {
		global.MoLogger.Error("更新角色状态失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_RoleStatusUpdateFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_CommUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// GetRolePage 获取角色列表
// @Tags 角色管理
// @Summary 获取角色列表
// @Security ApiKeyAuth
// @Param data query s1.SysRolePageReq true "角色信息"
// @Success 200 {object} response.Response{data=response.PageResult} "{"code":200,"data":{},"msg":"获取成功"}"
// @Router /system/role/list [get]
func (s *SysRoleCtrl) GetRolePage(c *gin.Context) {
	pageReq := &s1.SysRolePageReq{}
	if err := c.ShouldBindQuery(pageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	pageReq.ShouldBindParams(c)
	if pageResp, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.GetRolePage(c, pageReq); err != nil {
		global.MoLogger.Error("获取角色列表失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_RoleListFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResp)
		return
	}
}

// GetRoleById 根据id获取角色信息
// @Tags 角色管理
// @Summary 根据id获取角色信息
// @Security ApiKeyAuth
// @Param id path string true "角色id"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"获取成功"}"
// @Router /system/role/{id} [get]
func (s *SysRoleCtrl) GetRoleById(c *gin.Context) {
	idStr := c.Param("id")
	if id, err := strconv.ParseUint(idStr, 10, 64); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	} else {
		if role, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.GetRoleById(c, id); err != nil {
			global.MoLogger.Error("获取角色失败", zap.Error(err))
			response.FailErrorWithCodeAsI18n(c, err, consts.CommSelectFailCode, consts.TKey_RoleGetFailMsg)
			return
		} else {
			if role == nil {
				response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_CommSelectFailMsg)
			} else {
				response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, role)
			}
		}
	}
}

// GetDeptTreeByRole 根据角色id获取部门树
// @Tags 角色管理
// @Summary 根据角色id获取部门树
// @Security ApiKeyAuth
// @Param id path string true "角色id"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"获取成功"}"
// @Router /system/role/deptTree/{id} [get]
func (s *SysRoleCtrl) GetDeptTreeByRole(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	} else {
		if result, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.GetDeptTreeByRole(c, id); err != nil {
			global.MoLogger.Error("获取角色部门树失败", zap.Error(err))
			response.FailErrorWithCode(c, err, consts.CommSelectFailCode, consts.TKey_RoleGetDeptTreeFailMsg)
			return
		} else {
			response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, result)
		}
	}
}

// UpdateDataScope 更新数据权限
// @Tags 角色管理
// @Summary 更新数据权限
// @Security ApiKeyAuth
// @Param data body s1.SysRoleUpdateReq true "角色信息"
// @Success 200 {object} response.Response{data=s1.SysRoleAddReq} "{"code":200,"data":{},"msg":"更新成功"}"
// @Router /system/role/dataScope [put]
func (s *SysRoleCtrl) UpdateDataScope(c *gin.Context) {
	updateReq := &s1.SysRoleUpdateReq{}
	if err := c.ShouldBindJSON(updateReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.UpdateDataScope(c, updateReq); err != nil {
		global.MoLogger.Error("更新数据权限失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_AuthDataUpdateFailMsg)
		return
	} else if !ok {
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_CommUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// ExportRole 导出角色
// @Tags 角色管理
// @Summary 导出角色
// @Security ApiKeyAuth
// @Param data query s1.SysRolePageReq true "角色信息"
// @Success 200 {object} response.Response{msg=string} "{"code":200,"data":{},"msg":"获取成功"}"
// @Router /system/role/export [post]
func (s *SysRoleCtrl) ExportRole(c *gin.Context) {
	s.IgnoreLogResponseData(c)
	pageReq := &s1.SysRolePageReq{}
	if err := c.ShouldBind(pageReq); err != nil {
		global.MoLogger.Info("参数错误", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if buf, err := services.ServiceGroupInst.SystemGroupInst.SysRoleServiceInst.ExportRole(pageReq); err != nil {
		global.MoLogger.Error("导出角色失败", zap.Error(err))
		response.FailErrorWithCode(c, err, consts.CommSelectFailCode, consts.TKey_RoleExportFailMsg)
		return
	} else {
		response.SuccessWithFileData(c, fmt.Sprintf(consts2.Excel_Export_Role, time.Now().Unix()), buf)
	}
}
